package com.itheima.app.manager;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.itheima.app.interceptor.UserHolder;
import com.itheima.autoconfig.HuanXin.HuanXinTemplate;
import com.itheima.domain.db.Announcement;
import com.itheima.domain.db.Question;
import com.itheima.domain.db.UserInfo;
import com.itheima.domain.mongo.Comment;
import com.itheima.domain.mongo.Friend;
import com.itheima.domain.vo.AnnouncementVo;
import com.itheima.domain.vo.ContactVo;
import com.itheima.domain.vo.PageBeanVo;
import com.itheima.domain.vo.UserCommentVo;
import com.itheima.service.mongo.CommentService;
import com.itheima.service.mongo.FriendService;
import com.itheimma.service.db.AnnouncementService;
import com.itheimma.service.db.QuestionService;
import com.itheimma.service.db.UserInfoService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import java.util.*;

@Component
public class MessageManager {

    @Autowired
    private HuanXinTemplate huanXinTemplate;

    @DubboReference
    private FriendService friendService;

    @DubboReference
    private UserInfoService userInfoService;

    @DubboReference
    private CommentService commentService;

    @DubboReference
    private AnnouncementService announcementService;

    @DubboReference
    private QuestionService questionService;

    //打招呼
    public void sendMessage(String userId, String reply) {

        //返回的是整个消息，不仅包含了回答，还包含了问题，我的名称等
        // 1.拼接消息参数格式
        Map<String, String> param = new HashMap<>();
        param.put("userId", UserHolder.get().getId().toString()); // 登录用户id
        UserInfo userInfo = userInfoService.findById(UserHolder.get().getId());
        param.put("nickname", userInfo.getNickname()); // 登录用户昵称
        Question question = questionService.findById(Long.parseLong(userId));
        if (question == null) {
            question = new Question();
        }
        param.put("strangerQuestion", question.getStrangerQuestion()); // 佳人陌生人问题
        param.put("reply", reply); // 你的回答

        huanXinTemplate.sendMsg(userId, JSON.toJSONString(param));
    }

    //聊一下，添加好友
    public void addFriend(String friendId) {
        Long userId = UserHolder.get().getId();
        //给mongoDB数据库中添加参数
        friendService.addFriend(userId, Long.parseLong(friendId));

        //给环信中添加联系人
        huanXinTemplate.addContacts(userId, Long.parseLong(friendId));
    }

    public ResponseEntity findContacts(Integer pageNum, Integer pageSize) {
        Long userId = UserHolder.get().getId();
        PageBeanVo pageBeanVo = friendService.findContacts(userId, pageNum, pageSize);

        List<Friend> friends =(List<Friend>) pageBeanVo.getItems();

        ArrayList<ContactVo> contactVos = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(friends)) {
            for (Friend friend : friends) {
                UserInfo userInfo = userInfoService.findById(friend.getFriendId());

                ContactVo contactVo = new ContactVo();
                contactVo.setUserInfo(userInfo);
                contactVo.setUserId(friend.getFriendId().toString());

                contactVos.add(contactVo);
            }
        }
        pageBeanVo.setItems(contactVos);

        return ResponseEntity.ok(pageBeanVo);
    }


    //喜欢数
    public ResponseEntity findLikeNum(Integer pageNum, Integer pageSize, Integer commentType) {
        Long publishUserId = UserHolder.get().getId();
        PageBeanVo pageBeanVo = commentService.findCommentNum(commentType, publishUserId, pageNum, pageSize);

        List<Comment> commentList =(List<Comment>) pageBeanVo.getItems();

        ArrayList<UserCommentVo> vos = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(commentList)) {
            for (Comment comment : commentList) {
                //获取操作人id
                UserInfo userInfo = userInfoService.findById(comment.getUserId());

                UserCommentVo userCommentVo = new UserCommentVo();
                userCommentVo.setAvatar(userInfo.getAvatar());
                userCommentVo.setId(userInfo.getId().toString());
                userCommentVo.setNickname(userInfo.getNickname());
                //时间格式得转换一下
                userCommentVo.setCreateDate(DateUtil.formatDateTime(new Date(comment.getCreated())));

                vos.add(userCommentVo);
            }
        }

        pageBeanVo.setItems(vos);

        return ResponseEntity.ok(pageBeanVo);
    }

    //查找公告
    public ResponseEntity findAnnouncement(Integer pageNum, Integer pageSize) {

        PageBeanVo pageBeanVo = announcementService.findAnnouncement(pageNum, pageSize);
        //查出来的Announcement需要封装到vo中
        List<Announcement> announcementList =(List<Announcement>) pageBeanVo.getItems();

        ArrayList<AnnouncementVo> vos = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(announcementList)) {
            for (Announcement announcement : announcementList) {
                AnnouncementVo vo = new AnnouncementVo();
                vo.setId(announcement.getId().toString());
                vo.setCreateDate(DateUtil.formatDateTime(announcement.getCreated()));
                vo.setDescription(announcement.getDescription());
                vo.setTitle(announcement.getTitle());
                vos.add(vo);
            }
        }

        pageBeanVo.setItems(vos);

        return ResponseEntity.ok(pageBeanVo);
    }
}
